package com.dreamlin.gankio.base.ui

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView

open abstract class BaseAdapter<Binding : ViewDataBinding, Bean>(
    private var mData: List<Bean>,
    @LayoutRes protected var layoutId: Int
) :
    RecyclerView.Adapter<BaseViewHolder<Binding>>(), View.OnClickListener {

    var mItemClickListener: ItemClickListener<Bean>? = null

    fun setItemClickListener(listener: ItemClickListener<Bean>) {
        this.mItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Binding> {
        val binding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            layoutId, parent, false
        ) as Binding
        mItemClickListener?.let {
            binding.root.setOnClickListener(this)
        }
        return BaseViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mData.size
    }


    override fun onClick(v: View?) {
        if (v?.tag is Int) {
            mItemClickListener?.onItemClick(v, mData[v.tag as Int])
        }
    }

}

/**
 * @param Binding itemViewBinding
 * @param Bean data type
 * @param ItemDiffCallBack item diff callback
 */
open abstract class DiffBaseAdapter<Binding : ViewDataBinding, Bean,
        ItemDiffCallBack : DiffUtil.ItemCallback<Bean>>(
    @LayoutRes protected var layoutId: Int,
    itemDiffCallback: ItemDiffCallBack
) : RecyclerView.Adapter<BaseViewHolder<Binding>>(), View.OnClickListener {

    protected val mDiffer = AsyncListDiffer(this, itemDiffCallback)

    var mItemClickListener: ItemClickListener<Bean>? = null

    fun setItemClickListener(listener: ItemClickListener<Bean>) {
        this.mItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Binding> {
        val binding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            layoutId, parent, false
        ) as Binding
        mItemClickListener?.let {
            binding.root.setOnClickListener(this)
        }
        return BaseViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDiffer.currentList.size
    }

    open fun submitList(newList: List<Bean>) {
        mDiffer.submitList(newList)
    }


    override fun onClick(v: View?) {
        if (v?.tag is Int) {
            mItemClickListener?.onItemClick(v, mDiffer.currentList[v.tag as Int])
        }
    }

}

class BaseViewHolder<T : ViewDataBinding>(val binding: T) : RecyclerView.ViewHolder(binding.root) {

    private val views: SparseArray<View> = SparseArray()

    fun <V> getView(@IdRes id: Int): V {
        var view = views[id]
        view = view ?: itemView.findViewById(id)
        views.put(id, view)
        return view as V
    }

    fun setText(@IdRes id: Int, text: String) {
        var view = views[id]
        if (view == null)
            view = itemView.findViewById(id)
        if (view is TextView) view.text = text
    }
}

interface ItemClickListener<T> {
    fun onItemClick(view: View, bean: T)
}
